package com.weibo.dip.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.PullImageResultCallback;

import java.util.concurrent.TimeUnit;

/** Created by haisen on 2018/6/25. */
public class DockerImagepull {

  private static DockerClient dockerClient = DockerClientBuilder.getInstance().build();

  public static boolean pullImage(String imagename, String imagetag) {
    boolean bool = false;
    try {
      bool =
          dockerClient
              .pullImageCmd(imagename)
              .withTag(imagetag)
              .exec(new PullImageResultCallback())
              .awaitCompletion(300, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
      e.printStackTrace();
      System.out.println("pull error");
    }
    return bool;
  }

  public static void main(String[] args) {
    if (args.length < 2) {
      System.out.println("error input");
      return;
    }
    String name = args[0];
    String tag = args[1];
    if (DockerImagepull.pullImage(name, tag)) {
      System.out.print("Pull image success");
    } else {
      System.out.print("Pull image fail");
    }
  }
}
